import {
  Request
} from "../../utils/request.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //
    typeindex: '-1',
    //高级信息
    fatherList:[],
    open:1,//切换基本信息、高级信息、印象
    statusBarHeight: getApp().globalData.statusBarHeight,
    navbarData: {
      showCapsule: 1
    },
    _current:0,//初始Swiper
    height:'100vh',
    typsstatus: '0', //切换分类显示状态

  },

  /**
   * 点击显示高级信息(二级)
   */
   showtypes: function (index) {
    if (this.data.typeindex != index.currentTarget.dataset.index) {
      this.data.typeindex = index.currentTarget.dataset.index
    } else if (this.data.typeindex == index.currentTarget.dataset.index) {
      this.data.typeindex = 'null'
    }
    this.setData({
      typeindex: this.data.typeindex
    });
       //调用之前关闭
       clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
       //开启定时器 铺获
       this._MonitorPage('#rich_two');

       //调用接口，显示高级信息
       new Request('senior/Get_Senior_List/GetLevel').request(
        () => {
          return {
            openid: app.globalData.openid,
            userid: this.userId,
            hei_id: this.data.fatherList[index.currentTarget.dataset.index].hei_id,
            level: '1'
          }
        },
        (res) => {
          // console.log(index.currentTarget.dataset.index)
          this.setData({
            secList: res.data.data
          })
        }); 
     
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.userId = options.userid;
    //获取高级信息列表(标题)
    new Request('senior/Get_Senior_List/GetSeniorInfo').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: this.userId,//朋友id
        }
      },
      (res) => {
        this.setData({
          fatherList: res.data.data
        })
      });
      
    //地址配置
    let url =   wx.getStorageSync("config");
    console.log(this.userId);
    // 获取用户好友基本信息
    new Request('friend/Get_Friend_Info/GetDetail').request(
      () => {
        return {
          openid: app.globalData.openid,
          user_id: this.userId, //朋友id
        }
      },
      (res) => {
        if(res.data.code == 0){
          new Request().showToast(res.data.msg);
          return;
        }
        console.log(res.data.user);

        let reg   = new RegExp("女");
        let SQLsex  = reg.test(res.data.user.choose_gender);
        //用户头像
        let avatar = url.service.host + res.data.user.customer_avatar;
        this.setData({
          basicList: res.data.data,
          userInfo: res.data.user,
          SQLsex: SQLsex,
          avatar: avatar,
        });
      });

      //获取好友印象列表信息
      new Request('impress/Get_Friend_Impress/GetList').request(
        () => {
          return {
            openid: app.globalData.openid,
            obj_id: this.userId, //朋友id
          }
        },
        (res) => {
          if(res.data.code == 0){
            this.setData({
              impressScore: 100,
            });
            return;
          }
          this.setData({
            impressList: res.data.data,
            impressScore: res.data.impressScore,
          });
        }
      );

      //查询当前好友与自己有没有关注的
      new Request('friend/Get_Follow/GetFollow').request(
        () => {
          return {
            openid: app.globalData.openid,
            userid: app.globalData.userid,
            object_id: this.userId, //朋友id
          }
        },
        (res) => {
          if(res.data.data == 0){
            this.setData({
              guangzhuType: true,//没有关注
            });
          }else if(res.data.data == 1){
            this.setData({
              guangzhuType: false,//关注了用户
            });
          }
        }
      );

    // /**
    //  * 获取印象分
    //  */
    // // new Request('Zyyxiang/getyxnum').request(){
      new Request('Zyyxiang/getyxnum').request(
        () => {
          return {
            openid: this.sendfriendopenid == undefined ? app.globalData.openid : this.sendfriendopenid,
            userId: this.userId, //朋友id
          }
        },
        (res) => {
          console.log(res);
          this.setData({
            yxnum: res.data
            
          })
        });
        //查询朋友是否有屏蔽  和 关注 
      new Request('block/Get_Block/GetBlock').request(
        () => {
          return {
            openid: app.globalData.openid,
            userid: app.globalData.userid,
            object_id: this.userId, //朋友id
          }
        },
        (res) => {
          if(res.data.data == 0){
            this.setData({
              pingbiType: true,//没有屏蔽
            });
          }else if(res.data.data == 1){
            this.setData({
              pingbiType: false,//屏蔽了用户
            });
          }
          
        });
        return;
    //     /**
    //      * 获取高级信息
    //      */

        // new Request('Zycolumnlist/getcolumnopt').request(
        //   () => {
        //     return {
        //       openid: app.globalData.openid,
        //       userId: app.globalData.userid
        //     }   
        //   },
        //   (res) => {
        //     console.log(res);
        //     // var gjlist=  JSON.parse(res.data);
        //     var gjlist= res.data;
        //     console.log(gjlist);
        //     this.setData({
        //       heightinfo: gjlist
        //     });
        //   });

    /**
     * 获取朋友基本消息
     */
    new Request('Zhaon_Get_Friend_Info/friendbasc').request(
      () => {
        return {
          userId: this.userId, //朋友id
        }
      },
      (res) => {
        if(res.data.data == '' || res.data.data == null){
          return;
          wx.showToast({
            title: '信息出错',
            icon: 'loading',
            duration: 1500,
            mask: true,
            complete: ()=>{
              setTimeout(()=>{
                wx.navigateBack({
                  delta: 1
                });
              },1501);
            }
          });
          return;
        }

        this.herBascId = res.data.msg.herBascId; //获取当前 对象 我的基本信息id
        this.herWantId = res.data.msg.herWantId; //获取当前 对象 想找基本信息id
        this.setData({
          HerBasicList: res.data.data, //原始数组
        });
        res.data.data.shift();      //裁剪第一个数组
        res.data.data.splice(0,1);  //裁剪第一个数组
        this.setData({
          HerBasicLists: res.data.data, //裁剪 ID 和 昵称 后的数组
        });
      });
      
      // //获取朋友印象消息
      new Request('Zyfriendinfo/friendyinxianlist').request(
        () => {
          return {
            // openid: this.sendfriendopenid == undefined ? app.globalData.openid : this.sendfriendopenid,
            userId: this.userId, //朋友id
          }
        },
        (res) => {
          this.setData({
            yinxiangList:res.data.data
          });
        });
        
        //获取朋友基本信息
        new Request('Zyfriendinfo/getfriendinfo').request(
          () => {
            return {
              // openid: this.sendfriendopenid == undefined ? app.globalData.openid : this.sendfriendopenid,
              userId: this.userId, //朋友id
            }
          },
          (res) => {
            if(res.data.data != ''){
              let sex = "女";
              let reg = new RegExp(sex);
              // let home = res.data.data.home.split(",");
              this.setData({
                // userInfo: res.data.data,
                // sexType: reg.test(res.data.data.sex),
                // userHome: home[0],
              });
            }
            
            
          });
          
            //查询朋友是否有关注 
            // 报错先注销
            return;
            new Request('Zyfriendinfo/guangzhuUserid').request(
              () => {
                return {
                  openid: app.globalData.openid,
                  userId: this.userId, //朋友id
                }
              },
              (res) => {
                if(res.data.data == 1){
                  this.setData({
                    guangzhuType: true,//没有关注
                  });
                }else if(res.data.data == -1){
                  this.setData({
                    guangzhuType: false,//关注了用户
                  });
                }
                
              });
  },
  /**切换基本信息、高级信息、印象
   * 
   */
  qietap:function(param){
    if(param.currentTarget.id == 1){
      this.data.open = 1;
    }else if(param.currentTarget.id == 2){
      this.data.open = 2;
    }else if(param.currentTarget.id == 3){
      this.data.open = 3;
    }
    this.setData({
      open: this.data.open,
      _current: (this.data.open - 1),
    });
  },
  /**
   * 点击显示 用户信息设置
   */
  setings:function(param){
    if(param.currentTarget.id){
      this.setData({
        seting:null,
      });
    }else{
      this.setData({
        seting:true,
      });
    }
  },
  /**
   * 点击屏蔽用户按钮操作
   */
  pingbiButton:function(){
    if(this.data.pingbiType){  //没有屏蔽的操作逻辑
      this.setData({
        pingbiType:false,
      });
      new Request('block/Update_Block/Update').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
          object_id: this.userId, //朋友id
          pingbiType:1, //去屏蔽用户
        }
      },
      (res) => {
        if(res.data.code == 0){
          new Request().showToast('操作失败');
          return;
        }
        new Request().showToast('操作成功');
      });
    }else{
      this.setData({
        pingbiType:true,
      });
      new Request('block/Update_Block/Update').request(
        () => {
          return {
            openid: app.globalData.openid,
            userid: app.globalData.userid,
            object_id: this.userId, //朋友id
            pingbiType: '-1', //去屏蔽用户
          }
        },
        (res) => {
          if(res.data.code == 0){
            new Request().showToast('操作失败');
            return;
          }
          new Request().showToast('操作成功');
        });
    }
    
  },
  //关注用户按钮操作
  guanzuButton:function(){
  if(this.data.guangzhuType){  //没有屏蔽的操作逻辑
        this.setData({
          guangzhuType:false,
        });
        new Request('friend/Update_Follow/Update').request(
        () => {
          return {
            openid: app.globalData.openid,
            userid: app.globalData.userid,
            object_id: this.userId, //朋友id
            guangzhuType:1, //去屏蔽用户
          }
        },
        (res) => {
          if(res.data.code == 0){
            new Request().showToast('操作失败');
            return;
          }
          new Request().showToast('操作成功');
        });
      }else{
        this.setData({
          guangzhuType:true,
        });
        new Request('friend/Update_Follow/Update').request(
          () => {
            return {
              openid: app.globalData.openid,
              userid: app.globalData.userid,
              object_id: this.userId, //朋友id
              guangzhuType: '-1', //去屏蔽用户
            }
          },
          (res) => {
            if(res.data.code == 0){
              new Request().showToast('操作失败');
              return;
            }
            new Request().showToast('操作成功');
          });
      }
  },
  gotoPipei:function(){
    new Request().showToast('敬请期待哟~');

    return;

    wx.navigateTo({
      url: '/pages/zhaon_pipei/zhaon_pipei?userBaseId='+ this.herBascId + '&herBascId='+this.herBascId + '&herWantId='+this.herWantId+'&userid='+this.userId,
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {

        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('getuserinfo', { data: this.data.userInfo,userid:this.userId })
      }.bind(this)
    })
  },
   // 返回上一页面
  _navback() {
    wx.navigateBack()
  },
  /**
   * 跳转到聊天界面
   */
  gotoContact:function(e){
    console.log(e) //uid点击进入聊天对象的界面
    wx.navigateTo({
      url: '/pages/zhaon_xiaoxi/zhaon_xiaoxi_contact/zhaon_xiaoxi_contact?uid=' + this.userId + '&uimg=' + this.data.avatar + '&name=' + this.data.userInfo.customer_name,
    });
  },
  /**
   * 跳转到备注页面
   */
  beizhu:function(){
    wx.navigateTo({
      url: '/pages/zhaon_friendinfo/zhaon_friendinfo_beizhu/zhaon_friendinfo_beizhu',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { userId: this.userId });
        this.setData({
          seting:null,
        });
      }.bind(this),
    });
  },
  /**
   * 跳转到评印象页面 userId: this.userId, //朋友id
   */
  yingxiang:function(){
    wx.navigateTo({
      url: '/pages/zhaon_friendinfo/zhaon_friendinfo_yingxiang/zhaon_friendinfo_yingxiang',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { userId: this.userId });
        this.setData({
          seting:null,
        });
      }.bind(this),
    })
  },
  /**
   * swiper滑块切换
   */
  _bindchange:function(res){
    this.setData({
      open: (res.detail.current + 1),
      _current:res.detail.current,
    });
    if(res.detail.current == 0){
      this.data.open = 1;
      if(this.data.basicCode != 2){
        this.setData({
          open: this.data.open,
        });
        //开启定时器 铺获
        //调用之前关闭
        clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        //开启定时器 铺获
        this._MonitorPage('#userinfo-box-one');
      }
    }else if(res.detail.current == 1){
      this.data.open = 2;
      if(this.data.perfectCode != 2){
        this.setData({
          open: this.data.open,
        });
        //开启定时器 铺获
        //调用之前关闭
        clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        //开启定时器 铺获
        this._MonitorPage('#rich_two');
      }
    }else if(res.detail.current == 2){
      this.data.open = 3;
      if(this.data.perfectCode != 2){
        this.setData({
          open: this.data.open,
        });
        //开启定时器 铺获
        //调用之前关闭
        clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        //开启定时器 铺获
        this._MonitorPage('#userinfo-box-three');
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //开启定时器 铺获
    //调用之前关闭
    clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
    //开启定时器 铺获
    this._MonitorPage('#userinfo-box-one');
  },
    /**
   * 
   * @param {
    *监听基本心动列表 页面长度
    *page : 监听页面id元素
    *LogSetInterval: 初始化巡航铺获
    *LogSetIntNum: 记录巡航铺获15次退出
    *}
    */
   _MonitorPage:function(page,LogSetIntNum = 15,LogSetInterval = new Array()){
    this.SetIntNumberUserInfo_One =  setInterval(function(param){ //巡航铺获
      wx.createSelectorQuery().select(page).boundingClientRect(function(res){
        if(res == null){
          clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
          return;
        }
        let heightView = res.height + 300;
        if(app.globalData.windowHeight >= heightView){
          this.setData({
            height: (app.globalData.windowHeight - app.globalData.statusBarHeight*2 - 70) +'px',
          });
        }else{
          this.setData({
            height: (res.height + 150)+'px',
          });
        }
        LogSetInterval.push(1);
        if(LogSetInterval.length == LogSetIntNum){
          clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        }
      }.bind(this)).exec();
    }.bind(this),100);
   },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})